/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.clientserver;

/**
 *
 * @author Mr.Sam
 */
import java.net.Socket;
import java.io.IOException;
import java.util.TooManyListenersException;
import fr.ove.clientserver.SocketServer;
import fr.ove.clientserver.events.*;
import fr.ove.utils.Connection;
import fr.ove.utils.ConnectionHandler;

/**
* A server.<BR>
* After its instanciation, don't forget to set the connection handler. Otherwise,
* clients could not connect.
*/
public class Server implements SocketServerListener {
    /**
    * The socket server.
    */
    private SocketServer socketServer;
    
    /**
    * The connection handler for the server.
    */
    private ConnectionHandler handler;
    
    /**
    * The default constructor.<BR>
    * The default is the server running on port 6666 and allowing a
    * maximum of 300 connections.
    */
    public Server() throws IOException {
        this(6666, 300);
    }
    
    /**
    * The constructor.
    * @param portNumber the port number used on the host to run the server.
    * @param maxSocket the maximum number of connections allowed.
    */
    public Server(int portNumber, int maxConnection) throws IOException {
        socketServer = new SocketServer(portNumber, maxConnection);
        try {
            socketServer.addSocketServerListener(this);
        }
        catch (TooManyListenersException tmle) {
            tmle.printStackTrace();
        }
    }
    
    /**
    * Sets the connection handler for the server.
    * @param handler the connection handler.
    */
    public void setConnectionHandler(ConnectionHandler handler) {
        this.handler = handler;
    }
    
    /**
    * Returns the connection handler of the server.
    */
    public ConnectionHandler getConnectionHandler() {
        return handler;
    }
    
    // SocketServerListener interface implementation
    
    /**
    * Accept the socket encapsulated inside the event fired by the socket server listenned.
    * @param evt the event fired by the socket server listenned.
    */
    public void socketAccepted(SocketServerEvent evt) {
        Socket socket = evt.getSocket();
        if (handler != null) {
            Connection connection = new Connection();
            try {
                connection.open(socket);
                handler.handleConnection(connection);
            }
            catch (IOException ioe) {
                System.out.println("Failed to open the connection");
                ioe.printStackTrace();
            }
        }
        else {
            try {
                socket.close();
            }
            catch (IOException ioe) {
                System.out.println("Failed to close the socket");
                ioe.printStackTrace();
            }
        }
    }
}   